package com.ebeitech.auction.push.xiaomi;

import android.content.Context;
import android.os.Build;

import com.ebeitech.auction.BuildConfig;
import com.ebeitech.auction.application.EbeiApplication;
import com.ebeitech.auction.http.EbeiErrorCode;
import com.ebeitech.auction.http.HttpService;
import com.ebeitech.auction.http.service.HttpCommonServiceRx;
import com.ebeitech.library.constant.SPConstants;
import com.ebeitech.library.util.Log;
import com.ebeitech.library.util.SPManager;
import com.ebeitech.library.util.TimeUtil;
import com.huawei.android.pushagent.api.PushManager;
import com.xiaomi.mipush.sdk.MiPushClient;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class PushUtils {

	/**
	 * 启动推送服务
	 * 
	 * @param context
	 */
	public static void startPushService(Context context) {
		if (BuildConfig.FLAVOR.equals("build") && "huawei".equalsIgnoreCase(Build.MANUFACTURER)) {
			Log.i("---启动华为推送---");
			PushManager.requestToken(context);
		} else {//xiaomi 和其他类型的手机
			Log.i("---启动小米推送---");
			MiPushClient.registerPush(context, XMConstants.getXMAppId(),XMConstants.getXMAppKey());
		}
	}

	/**
	 * 停止推送服务
	 * 
	 * @param context
	 */
	public static void stopPushService(Context context) {
		if (BuildConfig.FLAVOR.equals("build") && "huawei".equalsIgnoreCase(Build.MANUFACTURER)) {
			Log.i("---停止华为推送---");
			PushManager.enableReceiveNotifyMsg(context, false);
		} else {
			Log.i("---停止小米推送---");
			MiPushClient.unregisterPush(context);

		}
	}

	/**
	 * @param state
	 * @return 获得发送我的登录状态的参数
	 */
	private static Map<String, String> sendMyStateUrlParams(String state) {
		String account = EbeiApplication.getUserAccount();
		String channelId =    SPManager.getInstance().get(SPConstants.SHARED_PRE_CHANNEL_ID_KEY,"");
		String phoneplatform =    SPManager.getInstance().get(SPConstants.SHARED_PRE_CHANNEL_STYLE,"");

		Map<String, String> valuePair = new HashMap<String, String>();
		valuePair.put("platform", phoneplatform);
		valuePair.put("deviceId", channelId);
		valuePair.put("dev", "0");//1开发环境，0或不设置此参数，正式环境
		return valuePair;
	}

	public static void sendMyStateUrl(Context context, String state) {
		Map<String, String> valuePair = PushUtils.sendMyStateUrlParams(SPConstants.LOGIN_STATE);
		HttpService mHttpService = HttpService.getInstance();
		HttpCommonServiceRx mHttpCommonServiceRx = mHttpService.getRxGsonRetrofit().create(HttpCommonServiceRx.class);
		Call<ResponseBody> call = null;
		if (SPConstants.LOGIN_STATE.equals(state)) {
			call = mHttpCommonServiceRx.doBindPush(valuePair);
		} else {
			call = mHttpCommonServiceRx.doUnBindPush(valuePair);
		}
		mHttpService.executeHttp(context, call,
				new HttpService.HttpListener<String>() {
					@Override
					public void onStart() {

					}

					@Override
					public void onError(EbeiErrorCode errorCode) {

					}

					@Override
					public void onSuccess(String result) {

					}
				}, String.class);
		Log.i("pushservice","state = " + state);
	}
}
